var fs = require('fs');


export const DataExport = (path, data, callback) => {
    fs.writeFile(path, JSON.stringify(data), {}, function(err) {
        if (err) {
            callback(err);
            return;
        }
        callback(false);
    });
}

export const DataImport = (path, callback) => {
    // 写入成功后读取测试
    fs.readFile(path, 'utf-8', function(err, data) {
        if (err) {
            callback(null);
        }
        callback(JSON.parse(data));
    });
}

export const CsvExport = (path, title, titleIndex, data, callback) => {
    let fileStr = title + '\n';
    for (let i = 0; i < data.length; i++) {
        const nodeInfo = data[i];
        const nodeInfoArr = [];
        for (let k = 0; k < titleIndex.length; k++) {
            nodeInfoArr.push(nodeInfo[titleIndex[k]]);
        }
        fileStr += nodeInfoArr.join(',') + '\n'
    }
    fs.writeFile(path, '\ufeff' + fileStr, { encoding: 'utf8' }, function(err) {
        if (err) {
            callback(err);
            return;
        }
        callback(false);
    });
}